//
//  MGResponder.m
//  Express
//
//  Created by lhongl on 2025/9/28.
//  Copyright © 2025 QM. All rights reserved.
//

#import "MGResponder.h"
#import "MGModuleManage.h"
@implementation MGResponder

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions API_AVAILABLE(ios(3.0)) {
    // Override point for customization after application launch.
    for (id module in [MGModuleManage allModules]) {
        if ([module respondsToSelector:@selector(application:didFinishLaunchingWithOptions:)]) {
            [module application:application didFinishLaunchingWithOptions:launchOptions];
        }
    }
    return YES;
}

- (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions actions:(NSDictionary *)actions{
    for (id module in [MGModuleManage allModules]) {
        if ([module respondsToSelector:@selector(application:didFinishLaunchingWithOptions:actions:)]) {
            [module application:application didFinishLaunchingWithOptions:launchOptions actions:actions];
        }
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    for (id module in [MGModuleManage allModules]) {
        if ([module respondsToSelector:@selector(applicationWillResignActive:)]) {
            [module applicationWillResignActive:application];
        }
    }
}


- (void)applicationDidEnterBackground:(UIApplication *)application API_AVAILABLE(ios(4.0)) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    for (id module in [MGModuleManage allModules]) {
        if ([module respondsToSelector:@selector(applicationDidEnterBackground:)]) {
            [module applicationDidEnterBackground:application];
        }
    }
}


- (void)applicationWillEnterForeground:(UIApplication *)application API_AVAILABLE(ios(4.0)) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    for (id module in [MGModuleManage allModules]) {
        if ([module respondsToSelector:@selector(applicationWillEnterForeground:)]) {
            [module applicationWillEnterForeground:application];
        }
    }
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    for (id module in [MGModuleManage allModules]) {
        if ([module respondsToSelector:@selector(applicationDidBecomeActive:)]) {
            [module applicationDidBecomeActive:application];
        }
    }
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    for (id module in [MGModuleManage allModules]) {
        if ([module respondsToSelector:@selector(applicationWillTerminate:)]) {
            [module applicationWillTerminate:application];
        }
    }
}

#pragma mark - openURL
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation API_DEPRECATED_WITH_REPLACEMENT("application:openURL:options:", ios(4.2, 9.0)) API_UNAVAILABLE(visionos) API_UNAVAILABLE(tvos) {
    for (id module in [MGModuleManage allModules]) {
        if ([module respondsToSelector:@selector(application:openURL:sourceApplication:annotation:)]) {
#ifdef DEBUG
            NSLog(@"[MG_DEBUG] module %@ toggle openURL",module);
#endif

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
            return [module application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
        }
    }
    //    [DANavigator openURL:url.absoluteString];
    return YES;
}

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
    for (id module in [MGModuleManage allModules]) {
        if ([module respondsToSelector:@selector(application:openURL:options:)]) {
#ifdef DEBUG
            NSLog(@"[MG_DEBUG] module %@ toggle openURL",module);
#endif
            return [module application:app openURL:url options:options];
        }
    }
    //    [DANavigator openURL:url.absoluteString];
    return  YES;
}

#endif


#pragma mark - APNS
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken API_AVAILABLE(ios(3.0)) {
    for (id module in [MGModuleManage allModules]) {
        if ([module respondsToSelector:@selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)]) {
            [module application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
        }
    }
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error API_AVAILABLE(ios(3.0)) {
    for (id module in [MGModuleManage allModules]) {
        if ([module respondsToSelector:@selector(application:didFailToRegisterForRemoteNotificationsWithError:)]) {
            [module application:application didFailToRegisterForRemoteNotificationsWithError:error];
        }
    }
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo API_DEPRECATED("Use UserNotifications Framework's -[UNUserNotificationCenterDelegate willPresentNotification:withCompletionHandler:] or -[UNUserNotificationCenterDelegate didReceiveNotificationResponse:withCompletionHandler:] for user visible notifications and -[UIApplicationDelegate application:didReceiveRemoteNotification:fetchCompletionHandler:] for silent remote notifications", ios(3.0, 10.0)) API_UNAVAILABLE(visionos) {
    for (id module in [MGModuleManage allModules]) {
        if ([module respondsToSelector:@selector(application:didReceiveLocalNotification:)]) {
            [module application:application didReceiveRemoteNotification:userInfo];
        }
    }
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler API_AVAILABLE(ios(7.0)){
    for (id module in [MGModuleManage allModules]) {
        if ([module respondsToSelector:@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)]) {
            [module application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
        }
    }
}

@end
